package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;
import com.neutech.maplestory.util.MusicThread;

import java.awt.*;
import java.util.List;
import java.util.Random;

public class Mob extends AbstractMapleStoryObject {
    public static Image[] images = new Image[100];

    static {
        //怪物左面站着的
        for (int i = 0; i < 3; i++) {
            images[i] = ImageUtil.getImage("mob_left_stand" + i);
        }
        //怪物左面走的
        for (int i = 3; i < 7; i++) {
            images[i] = ImageUtil.getImage("mob_left_walk" + (i - 3));
        }
        //怪物左面死的
        for (int i = 7; i < 10; i++) {
            images[i] = ImageUtil.getImage("mob_left_die" + (i - 7));
        }
        //怪物右面站着的
        for (int i = 10; i < 13; i++) {
            images[i] = ImageUtil.getImage("mob_right_stand" + (i - 10));
        }
        //怪物右面走的图片
        for (int i = 13; i < 17; i++) {
            images[i] = ImageUtil.getImage("mob_right_walk" + (i - 13));
        }
        //怪物右面死的
        for (int i = 17; i < 20; i++) {
            images[i] = ImageUtil.getImage("mob_right_die" + (i - 17));
        }
        //怪物左面跳的
        images[20] = ImageUtil.getImage("mob_left_jump");
        //怪物右面跳
        images[21] = ImageUtil.getImage("mob_right_jump");
    }

    public int MAX_HP;
    public boolean jump;
    public int mobCount;
    Random random = new Random();

    public Mob() {
        this.height = images[0].getHeight(null);
        this.width = images[0].getWidth(null);
        this.dir = Direction.RIGHT;
        this.action = Action.WALK;
        this.live = true;
        this.MAX_HP = 20000;
        this.HP = MAX_HP;
        this.jump = false;
        this.mobCount = random.nextInt(10);
    }

    public Mob(MapleStoryClient msc, int x, int y, int speed) {
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    private int count;
    private int step;
    private int dieStep;
    public int mobNum;

    @Override
    public void draw(Graphics g) {
        msc.num++;
        if (dieStep >= 2) {
            this.live = false;
            dieStep = 0;
        }
        if (!live) {
            msc.mobs.remove(this);
            new MusicThread("Die.mp3", false).start();
            return;
        }
        switch (dir) {
            case LEFT:
                switch (action) {
                    case STAND:
                        if (count++ % 5 == 0) {
                            g.drawImage(images[step++ % 3], x, y, null);
                        } else {
                            g.drawImage(images[step % 3], x, y, null);
                        }
                        break;
                    case WALK:
                        /**
                         * 怪物左面走
                         *///
                        if (count++ % 5 == 0) {
                            g.drawImage(images[step++ % 4 + 3], x, y, null);
                        } else {
                            g.drawImage(images[step % 4 + 3], x, y, null);
                        }
                        break;
                    /**
                     * 怪物左面死
                     */
                    case DIE:
                        if (count++ % 5 == 0) {
                            g.drawImage(images[dieStep++ % 3 + 7], x, y, null);
                        } else {
                            g.drawImage(images[dieStep % 3 + 7], x, y, null);
                        }
                        break;
                    case JUMP:
                        /**
                         *怪物左面跳
                         */

                        g.drawImage(images[20], x, y, null);

                        break;
                    default:
                        break;
                }
                break;
            case RIGHT:
                switch (action) {
                    //
                    /**
                     * 怪物右面站着
                     */
                    case STAND:
                        if (count++ % 5 == 0) {
                            g.drawImage(images[step++ % 3 + 10], x, y, null);
                        } else {
                            g.drawImage(images[step % 3 + 10], x, y, null);
                        }
                        break;
                    /**
                     * 怪物右面走
                     */
                    case WALK:
                        if (count++ % 5 == 0) {
                            g.drawImage(images[step++ % 4 + 13], x, y, null);
                        } else {
                            g.drawImage(images[step % 4 + 13], x, y, null);
                        }
                        break;
                    /**
                     * 怪物右面死
                     */
                    case DIE:
                        if (count++ % 5 == 0) {
                            g.drawImage(images[dieStep++ % 3 + 17], x, y, null);
                        } else {
                            g.drawImage(images[dieStep % 3 + 17], x, y, null);
                        }
                        break;
                    /**
                     * 怪物右面跳
                     */
                    case JUMP:

                        g.drawImage(images[21], x, y, null);

                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
        if (this.HP < MAX_HP) {
            bloodBar.draw(g);
        }

        move();

    }
    private int jumpCount =0;
    @Override
    public void move() {
        if (this.x >= Constant.GAME_WINDOW_WIDTH - this.width) {
            this.dir = Direction.LEFT;
        } else if (this.x <= 0) {
            this.dir = Direction.RIGHT;
        }
        if (jump) {
            if (this.dir == Direction.LEFT){
                this.x -= speed*2 ;
                this.action = Action.JUMP;
            }
            if (this.dir == Direction.RIGHT){
                this.y += speed*2;
                this.action = Action.JUMP;
            }

        }else {
            jumpDown(msc.grounds);
        }
        switch (dir) {
            case LEFT:
                switch (action) {
                    case WALK:
                        this.x -= speed;
                        break;
                    case JUMP:
                        jump(msc.grounds);
                        break;
                    default:
                        break;
                }
                break;
            case RIGHT:
                switch (action) {
                    case WALK:
                        this.x += speed;
                        break;
                    case JUMP:
                        jump(msc.grounds);
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }

        if (jumpCount++ %100 == 0 ){
//            if (this.dir == Direction.RIGHT){
//                if (this.x >= 605 - this.width && this.x <= 775 ){
//                    jump = true;
//                }
//                if (this.x >= 1080 - this.width  && this.x <= 1250 - this.width){
//                    jump = true;
//                }
//                if (this.x >= 1585 - this.width  && this.x <= 1675 - this.width){
//                    jump = true;
//                }
//            }
//            if (this.dir == Direction.LEFT){
//                if (this.x <= 775  && this.x>= 605){
//                    jump = true;
//                }
//                if (this.x <= 1255  && this.x>= 1080 ){
//                    jump = true;
//                }
//                if (this.x<=1675 && this.x >= 1585 ){
//                    jump = true;
//                }
//            }
            jump = true;

        }else {
            this.action = Action.WALK;
        }
        outOfBund();
        confirmStatus();
    }
    public void confirmStatus(){
        if (this.HP <=0){
            this.action = Action.DIE;
        }

    }
    private void outOfBund() {
        if (this.x <= 0) {
            this.x = 0;
        } else if (this.x >= Constant.GAME_WINDOW_WIDTH - this.width) {
            this.x = Constant.GAME_WINDOW_WIDTH - this.width;
        }
        else if (this.dir == Direction.RIGHT){
            if (this.x >= 605 - this.width && this.y >= 540-this.height+ 40   && this.x <= 775  && this.y <= 780){
//                this.x = 605 - this.width;
                this.dir = Direction.LEFT;


            }
            if (this.x >= 1080 - this.width && this.y >= 540 - this.height + 40 && this.x <= 1250 - this.width&& this.y <= 780){
//                this.x = 1080 - this.width;
                this.dir = Direction.LEFT;


            }
            if (this.x >= 1585 - this.width && this.y >= 440 - this.height + 40 && this.x <= 1675 - this.width && this.y <= 780){
//                this.x = 1585 - this.width;
                this.dir = Direction.LEFT;

            }
//            if (this.x >= 1360 - this.width){
//                this.y = this.y - (this.x-1360);
//            }
        }
        else if(this.dir == Direction.LEFT){
            if (this.x <= 775 && y >= 540 && this.x>= 605  && this.y <= 780){
//                this.x = 775;
                this.dir = Direction.RIGHT;
            }
            if (this.x <= 1255  && this.x>= 1080  && this.y >= 540 - this.height + 40 && this.y <= 780){
//                this.x = 1250;
                this.dir = Direction.RIGHT;
            }
            if (this.x<=1675 && this.x >= 1585 && this.y >= 440 - this.height + 40 && this.y <= 780){
//                this.x = 1675;
                this.dir = Direction.RIGHT;
            }
            if (this.y <= 780 && this.y >=770 ){
                jumpDown(msc.grounds);
            }

        }

    }


    /**
     * 跳跃的方法
     */
    private double v0 = Constant.INIT_MOB_JUMP_SPEED;
    private double vt;
    private double g = 9.8;
    private double delta_height;
    private double t = 0.5;

    //    private void jump() {
//        vt = v0 - g * t;
//        v0 = vt;
//        delta_height = v0 * t;
//        y -= delta_height;
//        if (y >= 500) {
//            jump = false;
//            v0 = Constant.INIT_JUMP_SPEED;
//            vt = 0.0;
//            y = 500;
//        }
//    }
    private boolean drop = true;
    public void jump(List<Ground> grounds){
        if (!drop){
            vt = v0 - g * t;
            delta_height = v0 * t;
            v0 = vt;
            y -= delta_height;
            if (vt <=0){
                vt = 0.0;
                drop = true;
            }
        }else {
            jumpDown(grounds);
        }
    }
    public void jumpDown(List<Ground> grounds){
        vt = v0 + g * t;
        delta_height = v0 * t;
        v0 = vt;
        y += delta_height;
        for (int i = 0; i < grounds.size(); i++) {
            Ground ground = grounds.get(i);
            if (this.getRectangle().intersects(ground.getRectangle()) && this.y<=ground.y - this.height  + 80){
                if (this.y >= (ground.y - this.height)){
                    this.y = ground.y - this.height;
                    drop = false;
                    jump = false;
                    v0 = Constant.INIT_JUMP_SPEED;
                    vt = 0.0;
                    break;
                }
            }else {
                drop = true;
            }
        }
    }





    public BloodBar bloodBar = new BloodBar();

    /**
     * 内部类部分
     */
    class BloodBar extends AbstractMapleStoryObject {
        private Image image;

        public BloodBar() {
            this.width = Mob.this.width;
            this.height = 10;
            this.image = ImageUtil.getImage("mob_left_blood");
            this.width = image.getWidth(null);
        }

        @Override
        public void draw(Graphics g) {
            //内部类访问外部类相同属性名时，使用外部类.this.属性名
//            Color color = g.getColor();
//            g.drawRect(Mob.this.x,Mob.this.y - 15,Mob.this.width,height);
//            g.setColor(Color.red);
//            g.fillRect(Mob.this.x+1,Mob.this.y - 15,(int) (Mob.this.width * Mob.this.HP / Mob.this.MAX_HP),height-2);
//            g.setColor(color);
            int n = Mob.this.width / this.width * Mob.this.HP / Mob.this.MAX_HP;
            for (int i = 0; i < n; i++) {
                g.drawImage(image, Mob.this.x + (i * this.width), Mob.this.y - 15, null);
            }

        }
    }

}
























